layui.use(['layer','jquery'], function(){
  layer = layui.layer
  ,$ = layui.jquery;
  var arr = $('#allpic').attr('data-parameter');
  arr = arr.split('-');
  var catid = arr[0];
  var id = arr[1];
  var title = $('h1').text();
  var picList;
  layer.ready(function(){	
  	$.ajax({
  	  url:'/api.php?op=get_atlas&modelid=3&id='+id,
  	  async:false,
  	  type:'GET',
  	  dataType:'json',
  	  timeout: 1000,
  	  beforeSend: function(){
	  	  var index = layer.load(1, {
  	  	  shade: [0.1,'#fff'],
  	  	  time: 1000
	  	  });  	  	
	    },
  	  error: function(){
  	  	layer.alert('无法获取数据，请稍后重试！');
  	  },
  	  complete: function(){  
     	  layer.close(layer.index); 
      },
  	  success: function(json){
        for(var p in json){
          if(p <= 8){
            if(p == 0){
              var str = '<li><a class="cur" href="javascript:;" data-key="'+p+'"><img src="'+json[p].url+'?x-oss-process=style/listpic" alt="'+json[p].alt+'" /></a></li>'; 
           }else{
              var str = '<li><a href="javascript:;" data-key="'+p+'"><img src="'+json[p].url+'?x-oss-process=style/listpic" alt="'+json[p].alt+'" /></a></li>';
           }
          }else{
            var str = '<li><a href="javascript:;" data-key="'+p+'"><img lay-src="'+json[p].url+'?x-oss-process=style/listpic" alt="'+json[p].alt+'" /></a></li>';
          }
          $('.layui-pic-list').append(str);  
        }
        picList = json;
  	  }
  	});
    var piccount = Object.keys(picList).length;
    $('.layui-pic-list li a').on('click', function(){
      var index = $('.layui-pic-list li a').index(this);
      var curnumber = $(this).attr('data-key');
      $('.layui-pic-list li a').removeClass('cur').eq(index).addClass('cur');
      $('#curpicnumber').html(parseInt(curnumber)+1);
      var showpic = layer.load(1, {
  	  	shade: [0.1,'#fff'],
  	  	time: 500
      });
      setTimeout(function(){
        $('.layui-pic-show a img').attr('src',picList[curnumber].url);
		    $('.layui-pic-show a img').attr('data-pic',curnumber);
        layer.close(showpic);
      },300);
    });	
  	$('#allpic').on('click',function(){
      var index = layer.load(1, {
  	  shade: [0.1,'#fff'],
  	  time: 1000
      });
      setTimeout(function(){
        layer.open({
  		    type: 2,
  		    title: title+'所有图片列表',
  		    shadeClose: true,
  		    shade: 0.8,
  		    area: ['1000px', '98%'],
  		    move:false,
  		    scrollbar: false,
  		    content: '/index.php?m=content&c=index&a=sroll&catid='+catid+'&id='+id
	  	  });
      },500); 
    });
    $('#bigpic').on('click', function(){
      var bigpic = $('#showcurpic img').attr('src');
      window.open(bigpic);
    });
    $('#showcurpic').on('click', function(){
      var netxkey = parseInt($(this).find('img').attr('data-pic'))+1;
      if(netxkey >= piccount){
        layer.confirm('已经是最后一张了！',{
          btn: ['返回图集', '上一图集', '下一图集'],
          yes: function(index, layero){
          	location.reload();
          },
          btn2: function(index, layero){
          	var prepic = $('.layui-pre').children("a").attr('href');
          	if(prepic.match('html')){
          	  location.href=prepic;
          	}else{
          	  layer.alert('暂无上一篇图集！');
          	}
          },
          btn3: function(index, layero){
          	var nexypic = $('.layui-next').children("a").attr('href');
          	if(nexypic.match('html')){
          	  location.href=nexypic;
          	}else{
          	  layer.alert('暂无下一篇图集！');
          	}
          },
		  btnAlign: 'c',
		  move: false,
		  shade: 0.8,
		  skin: 'layui-tips',
		  anim: 0
        });
		return false;
      }
      $('.layui-pic-list li').find('a').removeClass('cur');
      $('.layui-pic-list li a').each(function(){
        if($(this).attr('data-key')==netxkey){
          $(this).addClass('cur');
        }
      });
      $('#curpicnumber').html(parseInt(netxkey)+1);	
      var netxpic = layer.load(1, {
        shade: [0.1,'#fff'],
        time: 1000
      });
      setTimeout(function(){
		    $('#showcurpic img').attr('src',picList[netxkey].url);
		    $('#showcurpic img').attr('data-pic',netxkey);	
		    layer.close(netxpic);
      },500);
      var curkey = $('.layui-pic-list li .cur').attr('data-key');
      var curoffset = $('.layui-pic-list li .cur').offset().top;
      if(parseInt(curkey)%3 == 0){
        var mtop = 171 * parseInt(curkey)/3;
        $('.layui-pbox').animate({scrollTop: mtop},500);  
      }
    });
    $('#copy').on('click', function(){
      var url = $('#url').val();
      $('#url').select();
      document.execCommand("Copy");
      layer.tips('复制成功！', this, {
        tips: [1, '#E74291'],
        time: 1000
      });		  
    });
  });
  $('.layui-warp').on('click','.layui-heart',function(){
    var did = $(this).attr('data-id');
    var sid = GetCookie('heartid');
    if(sid == did){
     layer.alert('您已添加过喜欢此图集！',{
      skin: 'layui-tips',
      anim: 4
     });
    }else{
     $.post('/index.php?m=content&c=index&a=enjoy', {modelid:3, id: did}, function(str){
      SetCookie('heartid',did,1);
      layer.alert('(*^__^*)，谢谢你的喜欢！',{
      skin: 'layui-tips',
      anim: 1
       });
      $('#enjoy').html(str+'人喜欢');
     });
    }
  });
  $('#change').on('click', function(){
    $('.layui-rec-list').empty();
    $('#change').hide();
    $('.layui-rec-list').append('<i id="layui-rec-load" class="layui-icon layui-anim layui-anim-rotate layui-anim-loop" style="font-size: 36px;">&#xe63d;</i>');
    setTimeout(function(){
    $.ajax({
      url:'/api.php?op=get_rec&catid='+catid,
      type:'GET',
      dataType:'json',
      timeout: 1000,
      beforeSend: function(){
        
      },
      error: function(){
        layer.alert('无法获取数据，请稍后重试！');
      },
      complete: function(){  
        $('#layui-rec-load').hide();
        $('#change').show();
      },
      success: function(res){
        for(var p in res){
          var str = '<li><span>'+res[p].inputtime+'</span><a href="'+res[p].url+'" title="'+res[p].title+'" target="_blank">'+res[p].cutname+'</a></li>';
          $('.layui-rec-list').append(str);
        }
      }
    }); 
  },500); 
  });
});
function add_favorite(id,modelid){
layui.use(['layer','jquery'], function(){
  layer = layui.layer
  ,$ = layui.jquery;
    //$('.layui-favorite').find('i').html('&#xe658;');
    $.get('/api.php?op=add_favorite', {id:id, modelid:modelid},function(data){
    if(data==1){
      layer.msg('收藏成功！', {
      icon: 1,
      time: 2000 //2秒关闭（如果不配置，默认是3秒）
      });
    }
    if(data==2){
      layer.open({
        type: 2,
        title: '快速登录',
        shadeClose: true,
        shade: 0.8,
        area: ['320px', '296px'],
        move:false,
        scrollbar: false,
        content: '/index.php?m=member&c=index&a=fastlogin&forward='+encodeURIComponent(location.href)+'&siteid=1'
      });
    }
    if(data==4){
      layer.msg('已添加过收藏，请勿反复添加！', {
      icon: 2,
      time: 2000 //2秒关闭（如果不配置，默认是3秒）
      });
    }
    });
  });
}
layui.use('flow', function(){
  var flow = layui.flow;
  flow.lazyimg({
    elem: '.layui-pic-list li a img',
    scrollElem: '.layui-pbox'
  });
});
layui.use('flow', function(){
  var flow = layui.flow;
  flow.lazyimg({
    elem: '.layui-guess li a img'
  });
  flow.lazyimg({
    elem: '.layui-box .layui-actions a img'
  });
});